local info=require("info")
local LaunchScene=class("LaunchScene",cc.load("mvc").ViewBase)
local Alert=require("app.public.Alert")
local FZutil=require("app.FZutil")
local FzFileUtils=require("app.public.FzFileUtils")
local Language=require("Language")

local XMLHR_Status_SUCCESS=200
function LaunchScene:onCreate()
    require("app.FzFixCocosBug")
    local dir=cc.Director:getInstance()
    dir:getScheduler():setTimeScale(1)              --正常速度
    local uimap=require("app.Login.BackgroundLayer").create()
    self:addChild(uimap.root)
    self.uimap=uimap

    if FZ_SHOW_HEALTHGAME and FZ_SHOW_HEALTHGAME==1 then 
        self:showHealthGame()
    else 
        self:loadInfo()
    end
end

function LaunchScene:showHealthGame()
    local healthmap=FZutil.createSceneNode("GeneralUIall/GenaralUIView_healthGame")
    self:addChild(healthmap.root,100)
    healthmap.root:setPosition(display.center)
    performWithDelay(self,function()
        self:loadInfo()
        healthmap.root:removeSelf()
    end,2)
end

function LaunchScene:loadInfo()
    performWithDelay(self,function()
--        if info.key then 
--            self:requestServerInfo()
--        else 
--            self:requestOriginInfo()
--        end
          self:checkComp()
    end,0.1)
end

function LaunchScene:requestServerInfo()
    local xhr = cc.XMLHttpRequest:new() -- http请求  
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT -- 响应类型  
    xhr:open("GET", info.server_info) -- 打开链接  

    -- 状态改变时调用  
    local function onReadyStateChange()  
        -- 显示状态文本  
        local statusString = "Http Status Code:"..xhr.statusText 
        release_print(statusString)
        if xhr.status==XMLHR_Status_SUCCESS then    --成功
            release_print(xhr.response)
            FzFileUtils.writeFile(FzFileUtils.server_info_filename,FzFileUtils.getDownloadPath(),xhr.response)
            self:requestOriginInfo()
        else
            Alert:show(Language.updater.title_info_not_get,Language.updater.msg_info_not_get,Alert.TYPE_YES_NO,function(isY)
                if isY then
                    self:requestServerInfo()
                else
                    self:getApp():exit()
                end
            end)
        end
    end  

    -- 注册脚本回调方法  
    xhr:registerScriptHandler(onReadyStateChange)  
    xhr:send() -- 发送请求 
end

function LaunchScene:requestOriginInfo()
    local xhr = cc.XMLHttpRequest:new() -- http请求  
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT -- 响应类型  
    if info.key then 
        local server_info=require("server_info")
        xhr:open("GET", server_info[info.key]) -- 新的获取info的方式  
    else
        xhr:open("GET", info.origin_url)       -- 旧的获取info的方式
    end
    -- 状态改变时调用  
    local function onReadyStateChange()  
        -- 显示状态文本  
        local statusString = "Http Status Code:"..xhr.statusText 
        release_print(statusString)
        if xhr.status==XMLHR_Status_SUCCESS then    --成功
            release_print(xhr.response)
            FzFileUtils.writeFile(FzFileUtils.origin_info_filename,FzFileUtils.getDownloadPath(),xhr.response)
            self:requestServerList()
        else
            Alert:show(Language.updater.title_info_not_get,Language.updater.msg_info_not_get,Alert.TYPE_YES_NO,function(isY)
                if isY then
                    self:requestOriginInfo()
                else
                	self:getApp():exit()
                end
            end)
        end
    end  

    -- 注册脚本回调方法  
    xhr:registerScriptHandler(onReadyStateChange)  
    xhr:send() -- 发送请求 
end

--请求服务器列表
function LaunchScene:requestServerList()
    local origin_info=require(FzFileUtils.getOriginInfoPath())
    local xhr = cc.XMLHttpRequest:new() -- http请求  
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON -- 响应类型  
    xhr:open("GET", origin_info.server_list) -- 打开链接  

    -- 状态改变时调用  
    local function onReadyStateChange()  
        -- 显示状态文本  
        local statusString = "Http Status Code:"..xhr.statusText 
        release_print(statusString)
        if xhr.status==XMLHR_Status_SUCCESS then    --成功
            release_print(xhr.response) 
            FzFileUtils.writeFile(FzFileUtils.server_list_filename,FzFileUtils.getDownloadPath(),xhr.response)
            -- self:checkComp()
            self:requestFuncOpenConfig()
        else
            Alert:show(Language.updater.title_server_list_not_get,Language.updater.msg_server_list_not_get,Alert.TYPE_YES_NO,function(isyes)
                if isyes then
                    self:requestServerList()
                else
                    self:getApp():exit()
                end
            end)
        end
    end  

    -- 注册脚本回调方法  
    xhr:registerScriptHandler(onReadyStateChange)  
    xhr:send() -- 发送请求 
end

--请求功能开放配置
function LaunchScene:requestFuncOpenConfig()
    local origin_info=require(FzFileUtils.getOriginInfoPath())
    local xhr = cc.XMLHttpRequest:new() -- http请求  
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT -- 响应类型  
    xhr:open("GET", origin_info.open_config) -- 打开链接  

    -- 状态改变时调用  
    local function onReadyStateChange()  
        -- 显示状态文本  
        local statusString = "Http Status Code:"..xhr.statusText 
        release_print(statusString)
        if xhr.status==XMLHR_Status_SUCCESS then    --成功
            release_print(xhr.response) 
            FzFileUtils.writeFile(FzFileUtils.open_config_filename,FzFileUtils.getDownloadPath(),xhr.response)
            -- self:showAgreement()
            if self and self.checkComp then 
                self:checkComp()
            end
        else
            Alert:show(Language.updater.title_server_list_not_get,Language.updater.msg_server_list_not_get,Alert.TYPE_YES_NO,function(isyes)
                if isyes then
                    self:requestFuncOpenConfig()
                else
                    self:getApp():exit()
                end
            end)
        end
    end  

    -- 注册脚本回调方法  
    xhr:registerScriptHandler(onReadyStateChange)  
    xhr:send() -- 发送请求 
end

function LaunchScene:showAgreement()
    local uimap=FZutil.createSceneNode("GeneralUIall/GenaralUIView_agreement")
    self:addChild(uimap.root)
    uimap.root:setPosition(display.center)
    uimap.Button_yes:onTouch(function(e)
        if e.name=="ended" then 
            self:checkComp()
        end
    end)
    uimap.Button_no:onTouch(function(e)
        if e.name=="ended" then 
            self:getApp():exit()
        end
    end)
end

function LaunchScene:checkComp()
    self:getApp():enterScene("MainScene")
end


return LaunchScene